from loguru import logger
import sys

from config import settings


def configure_logging():
    settings.LOG_DIR.mkdir(exist_ok=True)
    logger.remove()

    # 控制台输出
    logger.add(
        sys.stderr,
        level=settings.LOG_LEVEL,
        format=settings.LOG_FORMAT,
        colorize=True,
        backtrace=True,
        diagnose=True
    )

    # 文件输出（仅按时间轮转）
    logger.add(
        str(settings.LOG_DIR / "face_api_{time:YYYY-MM-DD}.log"),  # 仅使用日期命名日志文件
        rotation="00:00",
        retention=f"{settings.LOG_RETENTION} days",
        compression="zip",
        level=settings.LOG_LEVEL,
        format=settings.LOG_FORMAT,
        backtrace=True,
        diagnose=True,
        enqueue=True
    )


# 初始化日志配置
configure_logging()

# 导出logger对象
log = logger
